//
// Created by dddgzc on 2025/2/5.
//

#include "../include/SunBullet.h"

SunBullet::SunBullet() {
    size.x = 96;
    size.y = 96;
    damage = 20;
    animationIdle.setAtlas(sunAtlas);
    animationIdle.setInterval(50);

    animationExplode.setAtlas(sunExplodeAtlas);
    animationExplode.setInterval(50);
    animationExplode.setIsLoop(false);
    animationExplode.setOnAnimationFinishedCallback([&]() {
        canRemove = true;
    });

    SDL_Texture *frameIdle = animationIdle.getFrame();
    SDL_Texture *frameExplode = animationExplode.getFrame();
    int idleWidth, idleHeight, explodeWidth, explodeHeight;
    SDL_QueryTexture(frameIdle, NULL, NULL, &idleWidth, &idleHeight);
    SDL_QueryTexture(frameExplode, NULL, NULL, &explodeWidth, &explodeHeight);
    explodeRenderOffset.x = (float) (idleWidth - explodeWidth) / 2.0f;
    explodeRenderOffset.y = (float) (idleHeight - explodeHeight) / 2.0f;
}

void SunBullet::onCollide() {
    Bullet::onCollide();
    camera.shake(5, 250);
    // 添加音效
}

void SunBullet::onUpdate(int delta) {
    Bullet::onUpdate(delta);
    if (valid) {
        velocity.y += gravity * (float) delta;
        position += velocity * (float) delta;
    }

    if (!valid) {
        animationExplode.onUpdate(delta);
    } else {
        animationIdle.onUpdate(delta);
    }

    if (checkIfExceedsScreen()) {
        canRemove = true;
    }
}

void SunBullet::onDraw(const Camera &camera) {
    Bullet::onDraw(camera);
    if (valid) {
        animationIdle.onDraw(camera, (int) position.x, (int) position.y);
    } else {
        animationExplode.onDraw(camera,
                                (int) (position.x + explodeRenderOffset.x),
                                (int) (position.y + explodeRenderOffset.y));
    }

    if (IS_DEBUG_MODE) {
        drawRectangle(camera, position.x, position.y, size.x, size.y, {255, 0, 0, 255});
    }
}
